<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Order
 *
 * @author wupeng
 */
include_once 'ApiAbstract.php';
include_once 'Rechange.php';

class Order extends ApiAbstract {

    //put your code here
    protected $_tradeNo;
    protected $_paybill;

    public function __construct($trade_no, $paybill, $periodStart, $account) {
        parent::__construct($periodStart, $account);
        $this->_tradeNo = $trade_no;
        $this->_paybill = $paybill;
    }

    protected function _addRechangeInfo() {
        $rechange = new Rechange($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
        $rechange->addTransaction();
        $account = Application_Model_UserAccount::load($this->_account->id);
        $this->_periodStart = $account->Money;
        return $this;
    }

    public function addTransaction() {
        $trades = $this->_getTrades();
        $periodStart = $this->_periodStart;
        if ($this->_paybill->payId != Application_Model_PayBill::PAY_ACCOUNT) {
            $this->_addRechangeInfo();
            $periodStart = $this->_account->Money;
        }
        $orderIds = explode(',', $this->_paybill->DomainId);
        $fees = $this->_getBusinessFee();
        $user = Application_Model_User::load($this->_paybill->AccountId);
        foreach ($orderIds as $_orderId) {
            $order = Application_Model_Order::load($_orderId);
            $businessTypes = $this->_getWebsiteBusinessCode();

            if (0 < $order->Discount) {
                $this->_addDiscountFinance($order,$user->nikename,$periodStart);
            }
            foreach ($businessTypes as $key => $_type) {
                $fee = $fees[$key];
                $amount = $order->$fee;
                if (0 >= $amount) {
                    continue;
                }
                $trade = $trades[$this->_paybill->payId];
                $data = array(
                    'transaction_number' => '',
                    'trader' => $trade,
                    'customer_id' => $this->_paybill->AccountId,
                    'member' => 'admin',
                    'customer_name' => $user->nikename,
                    'amount' => $amount,
                    'currency' => 'CNY',
                    'rate' => '1',
                    'rate_amount' => '0',
                    'period_start' => $periodStart,
                    'period_end' => $periodStart - $amount,
                    'business_type' => $_type,
                    'order_sn' => $order->OrderNum
                );
                $this->setFields($data);
                $url = $this->_buildUrl('addTransaction');
                $this->_updateMageFinance($url, $this->getFields());
                $periodStart -= $order->$fee;
            }
        }
    }

    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
    }

    protected function _addDiscountFinance($order,$nikename,&$periodStart) {
        $data = array(
            'transaction_number' => '',
            'trader' => 'backend-account-cr',
            'customer_id' => $this->_paybill->AccountId,
            'customer_name' => $nikename,
            'member' => 'admin',
            'amount' => $order->Discount,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $periodStart + $order->Discount,
            'business_type' => 'dg-discount-dr',
            'order_sn' => $order->OrderNum
        );
        $this->setFields($data);
        $url = $this->_buildUrl('addTransaction');
        $this->_updateMageFinance($url, $this->getFields());
        $periodStart += $order->Discount;
        
        return $this;
    }

    protected function _getWebsiteBusinessCode() {
        return array(
            'purchase_goods_price' => 'spzz-dr',
            'purchase_cn_ship' => 'gryf-dr'
        );
    }

    protected function _getBusinessFee() {
        return array(
            'purchase_goods_price' => 'productTotal',
            'purchase_cn_ship' => 'ShipFee'
        );
    }

}
